package com.py.credit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.py.credit.dao.DynamicSqlDao;
import com.py.credit.dao.DissentProcessDao;
import com.py.credit.dao.TableDao;
import com.py.credit.entity.Column;
import com.py.credit.entity.DissentProcess;
import com.py.credit.entity.Table;
import com.py.credit.service.DissentProcessService;
import com.py.credit.service.excel.read.service.InspectDataStyleService;
import com.py.framework.core.common.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author YaoS
 * @program project2.6
 * @classname DissentProcessServiceImpl
 * @description 前台异议处理服务实现
 * @date 2019-01-17 14:29
 */
@Service
public class DissentProcessServiceImpl implements DissentProcessService {
    @Autowired
    DissentProcessDao dissentProcessDao;
    @Autowired
    TableDao tableDao;
    @Autowired
    private DynamicSqlDao dynamicSqlDao;
    @Autowired
    @Qualifier("default")
    private InspectDataStyleService inspectDataStyleService;

    @Override
    public void add(DissentProcess dissentProcess) {
        dissentProcessDao.add(dissentProcess);
    }

    @Override
    public void saveFile(int id, String fileUrl) {
        dissentProcessDao.saveFile(id, fileUrl);
    }

    @Override
    public String selectDataOwner(String resourceId, String recordId) {
        return dissentProcessDao.selectDataOwner(resourceId, recordId);
    }

    @Override
    public DissentProcess getFrontDissentProcess(String id) {
        return dissentProcessDao.getFrontDissentProcess(id);
    }

    @Override
    public HashMap<String, Object> selectReviewData(String tableName, String recordId) {
        return dissentProcessDao.selectReviewData(tableName, recordId);
    }


    @Override
    public void updateStatus(int id, String status, String finishTime) {
        dissentProcessDao.updateStatus(id, status, finishTime);
    }

    @Override
    public Map<String, Object> selectRecordsById(String tableName, Integer recordId, List<String> showColumns) {
        return tableDao.selectRecordsById("t_" + tableName, recordId, showColumns);
    }

    @Override
    public void deleteData(int tableId, int recordId) {
        String tableName = tableDao.selectByPrimaryKey(tableId).getResourceId();
        tableDao.deleteData(tableName, recordId);
    }

    @Override
    public boolean updateData(int tableId, int recordId, String params, Table table, List<Column> columns) {


        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> jsonMap = JSONObject.parseObject(params);
        //取出所有的key和value，拼入list
        for (Object key : jsonMap.keySet()) {
            Map<String, Object> map = new HashMap<>(2);
            map.put("key", key);
            if (jsonMap.get(key).equals("") || jsonMap.get(key) == null) {
                map.put("value", null);
            }
            if (!jsonMap.get(key).equals("")) {
                map.put("value", jsonMap.get(key));
            }
            list.add(map);
        }
        //该步骤负责对数据进行重复性校验
        Map<String, Object> checkMap = inspectDataStyleService.duplicationCheck(list, table, columns, false, true);
        String message = String.valueOf(checkMap.get("duplicationMess"));
        if (null != checkMap && StringUtils.isNotEmpty(message)) {
            return false;
        }
        String MD5 = (String) jsonMap.get("MD5");
        Map<String, Object> md5Map = new HashMap<>(2);
        md5Map.put("key", "MD5");
        md5Map.put("value", MD5);
        list.add(md5Map);
        dynamicSqlDao.updateTableInfo("t_" + table.getResourceId(), recordId + "", list);
        return true;
    }


    @Override
    public List<Map<String, Object>> selectHistoryListForPage(String tyshxydm, String type, String startTime, String endTime, Page page) {
        return dissentProcessDao.selectHistoryListForPage(tyshxydm, type, startTime, endTime, page);
    }
}
